#coding=utf-8

from django import forms
from django.forms import ModelForm
from models import Options

class OptionsForm(ModelForm):
    ''' Forma do obsługi opcji konfiguracyjnych '''

    ORDINARY = 0
    SAFE = 1
    MODE_CHOICES = ( (ORDINARY, u'szybki'), (SAFE, u'bezpieczny'), )

    allow_tmp_msgs = forms.ChoiceField (choices=MODE_CHOICES)

    def __init__(self, *args, **kwargs):

        super(OptionsForm, self).__init__(*args, **kwargs)
        # zmien pola, ktore startuja jako puste
        self.fields['sessions'].required = False
        self.fields['msg_max_size'].required = False

    def field_clean(self, field_name):
        ''' pole field_name nie moze byc puste '''
        field = self.cleaned_data[field_name]
        if not field:
            raise forms.ValidationError(u"To pole jest wymagane!")

        return field
    
    def clean_sessions(self):
        ''' pole sessions nie moze byc puste '''
        return self.field_clean('sessions')

    def clean_msg_max_size(self):
        ''' pole sessions nie moze byc puste '''
        return self.field_clean('msg_max_size')
    
    class Meta:
        model = Options
        exclude = ('threads', 'archive_path')
